package com.kgc.dao;

import com.kgc.entity.Admin;
import com.kgc.entity.AdminDesc;

import java.sql.ResultSet;
import java.sql.SQLException;

public class AdminDao extends BaseDao{
//    //通过用户名和密码验证
//    public Admin selectAdmin(String username){
//        String sql = "select * from admin where username=? ";
//        try {
//            ResultSet rs = super.getExecuteQuery(sql, username);
//            if(rs.next()){
//                return new Admin(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getString(4),
//                        rs.getInt(5),rs.getString(6),rs.getString(7));
//            }
//        } catch (SQLException throwables) {
//            throwables.printStackTrace();
//        } finally {
//            super.closeConnection();
//        }
//        return null;
//    }
    //通过邮箱验证
    public Admin selectAdminByEmail(String email){
        String sql = "select * from admin where email=? ";
        try {
            ResultSet rs = super.getExecuteQuery(sql, email);
            if(rs.next()){
                return new Admin(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getString(4),
                        rs.getInt(5),rs.getString(6),rs.getString(7));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            super.closeConnection();
        }
        return null;
    }
    //查询业主总数
    public int totalUser(){
        String sql = "SELECT count(*) FROM user";
        try {
            ResultSet rs = super.getExecuteQuery(sql);
            if(rs.next()){
                return rs.getInt(1);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            super.closeConnection();
        }
        return 0;
    }
    //查询公告总数
    public int totalGongGao(){
        String sql = "SELECT count(*) FROM gonggao";
        try {
            ResultSet rs = super.getExecuteQuery(sql);
            if(rs.next()){
                return rs.getInt(1);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            super.closeConnection();
        }
        return 0;
    }
    //查询投诉总数
    public int totalTouSu(){
        String sql = "SELECT count(*) FROM tousu";
        try {
            ResultSet rs = super.getExecuteQuery(sql);
            if(rs.next()){
                return rs.getInt(1);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            super.closeConnection();
        }
        return 0;
    }
    //查询维修总数
    public int totalRepair(){
        String sql = "SELECT count(*) FROM repair";
        try {
            ResultSet rs = super.getExecuteQuery(sql);
            if(rs.next()){
                return rs.getInt(1);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            super.closeConnection();
        }
        return 0;
    }
    //查询房屋总数
    public int totalRoom(){
        String sql = "SELECT count(*) FROM room";
        try {
            ResultSet rs = super.getExecuteQuery(sql);
            if(rs.next()){
                return rs.getInt(1);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            super.closeConnection();
        }
        return 0;
    }
    //查询房屋闲置总数
    public int totalFreeRoom(){
        String sql = "SELECT count(*) FROM room where status=0";
        try {
            ResultSet rs = super.getExecuteQuery(sql);
            if(rs.next()){
                return rs.getInt(1);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            super.closeConnection();
        }
        return 0;
    }
    //查询车位总数
    public int totalCar(){
        String sql = "SELECT count(*) FROM car";
        try {
            ResultSet rs = super.getExecuteQuery(sql);
            if(rs.next()){
                return rs.getInt(1);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            super.closeConnection();
        }
        return 0;
    }
    //查询车位闲置总数
    public int totalFreeCar(){
        String sql = "SELECT count(*) FROM car where status=0";
        try {
            ResultSet rs = super.getExecuteQuery(sql);
            if(rs.next()){
                return rs.getInt(1);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            super.closeConnection();
        }
        return 0;
    }
    //查询已缴缴费总数
    public int totalUserPayment(){
        String sql = "SELECT count(*) FROM user_payment ";
        try {
            ResultSet rs = super.getExecuteQuery(sql);
            if(rs.next()){
                return rs.getInt(1);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            super.closeConnection();
        }
        return 0;
    }
    //查询车位闲置总数
    public int totalFreeUserPayment(){
        String sql = "SELECT count(*) FROM user_payment where status=0";
        try {
            ResultSet rs = super.getExecuteQuery(sql);
            if(rs.next()){
                return rs.getInt(1);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            super.closeConnection();
        }
        return 0;
    }

    public Admin select(){
        String sql = "SELECT username,phone,email FROM admin";
        try {
            ResultSet rs = super.getExecuteQuery(sql);
            if(rs.next()){
                return new Admin(rs.getString(1),rs.getString(2),rs.getString(3));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            super.closeConnection();
        }
        return null;
    }

    public int updateAdmin(Admin admin){
        String sql = "UPDATE admin SET email =?,username =?,phone =? WHERE `id` = 1;";
        return super.getExecuteUpdate(sql,admin.getEmail(),admin.getUsername(),admin.getPhone());
    }

    public String selectPwd(){
        String sql = "select password from admin ";
        try {
            ResultSet rs = super.getExecuteQuery(sql);
            if (rs.next()){
                return rs.getString(1);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            super.closeConnection();
        }
        return null;
    }

    public int updatePwd(String password){
        String sql = "update admin set password =?";
        return super.getExecuteUpdate(sql, password);
    }

    public AdminDesc selectAdminDesc(Integer id){
        String sql = "SELECT ad.id,a.username,a.email,ad.nickname,a.phone,ad.job,ad.tags,ad.content,ad.logo_path FROM admin a INNER JOIN admin_desc ad ON a.id=ad.admin_id WHERE a.id=?";
        try {
            ResultSet rs = super.getExecuteQuery(sql, id);
            if(rs.next()){
                return new AdminDesc(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getString(4),rs.getString(5),
                        rs.getString(6),rs.getString(7),rs.getString(8),rs.getString(9));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            super.closeConnection();
        }
        return null;
    }
    public int updateAdminImg(Integer adminId, String path) {
        String sql = "UPDATE `admin_desc` SET `logo_path` = ? WHERE `admin_id` = ? ";
        return super.getExecuteUpdate(sql, path, adminId);
    }
    public int updateAdminDesc(AdminDesc adminDesc) {
        String sql = "UPDATE `admin_desc` SET  `nickname` = ?, `job` = ?, `tags` = ?, `content` = ? WHERE `id` = ?";
        return super.getExecuteUpdate(sql, adminDesc.getNickname(), adminDesc.getJob(), adminDesc.getTags(), adminDesc.getContent(), adminDesc.getId());
    }
    public int updateEmailAndPhone(int id, String email, String phone) {
        String sql = "update admin set email=?,phone=? where id=?";
        return super.getExecuteUpdate(sql, email, phone, id);
    }
}
